package mamma.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mamma.dao.jinkyuDao;
import mamma.dao.sanghyunDao;
import mamma.vo.FriendUpdateVo;
import mamma.vo.MemberVo;
import mamma.vo.PhotoVo;
import mamma.vo.SearchVo;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import static org.apache.struts2.StrutsStatics.HTTP_REQUEST;
import static org.apache.struts2.StrutsStatics.HTTP_RESPONSE;

public class jinkyuAction extends ActionSupport{
	private MemberVo memberVo;					// the value object that has informations about the Member.
	private FriendUpdateVo friendVo;
	private SearchVo searchVo;
	
	private String email;						// the value that brings the user's info to the login page.
	private String password;					// the value that checks if the user's password is right.
	private String loginId;						// the value that has the login info.
	private String savedFile;
	private String requestEmail;
	
	private List<PhotoVo> photoList;			// the list that include the user's photos.
	private List<SearchVo> searchList;			// the list that include the result of searching members.
	private List<SearchVo> searchDetailList;
	private List<FriendUpdateVo> friendUpdateList; // the list that include the alarm of friend requests.
	private List<FriendUpdateVo> myRequestList;
	
	
	private int page;							// the photos loaded by this value.
	private int friendId;
	
	private String keyword;						// the value used when the user searchs mamma members.
	
	/*
	 * MEMBER ACTION
	 */
	
	// check the first access of the computer
	public String firstAccess(){
		System.out.println("FIRST ACCESS");
		ActionContext context = ActionContext.getContext();
		HttpServletRequest request = null;
		request = (HttpServletRequest)context.get(HTTP_REQUEST);
		Cookie[] cookie = request.getCookies();
		if(cookie != null){
			for(int i = 0; i < cookie.length; i++){
				if(cookie[i].getName().equals("firstAccess")){
					// if the computer has the cookie named "firstAccess", return "SUCCESS".
					// it means that the computer connected this site in the past.
					return SUCCESS;
				}
			}
			Cookie firstAccess = new Cookie("firstAccess", "true");
			firstAccess.setMaxAge(365*24*60*60);
			HttpServletResponse response = null;
			response = (HttpServletResponse)context.get(HTTP_RESPONSE);
			response.addCookie(firstAccess);
			
			return INPUT;
		}
		else{
			Cookie firstAccess = new Cookie("firstAccess", "true");
			firstAccess.setMaxAge(365*24*60*60);
			HttpServletResponse response = null;
			response = (HttpServletResponse)context.get(HTTP_RESPONSE);
			response.addCookie(firstAccess);
		}
		// this return named "INPUT" means the first access.
		return INPUT;
	}
	
	// search the email on our database
	public String emailCheck(){
		jinkyuDao dao = new jinkyuDao();
		boolean result = dao.emailCheck(email);
		// true of 'result' means that our database doesn't have this email.
		if(result == false){
			return ERROR;
		}
		return SUCCESS;
	}
	
	// login check
	public String loginCheck(){
		// if the user is logging in, just let him/her go the user's home.
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		memberVo = (MemberVo)session.get("login");
		
		return SUCCESS;
	}
	
	// insert the account
	public String insertAccount(){
		jinkyuDao dao = new jinkyuDao();
		dao.insertAccount(memberVo);
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		session.put("login", memberVo);
		
		return SUCCESS;
	}
	
	// login
	public String login(){
		memberVo = new MemberVo();
		memberVo.setEmail(email);
		memberVo.setPassword(password);
		
		jinkyuDao dao = new jinkyuDao();
		MemberVo temp = dao.selectAccount(memberVo);
		
		
		if(temp == null){
			return LOGIN;
		}
		memberVo = temp;
		
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		session.put("login", memberVo);
		
		return SUCCESS;
	}
	
	// logout
	public String logout(){
		System.out.println("logout activated");
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		session.clear();
		System.out.println("logout end");
		return SUCCESS;
	}
	
	// go the user's home
	public String goHome(){
		System.out.println(email);
		memberVo = new MemberVo();
		memberVo.setEmail(email);
		jinkyuDao dao = new jinkyuDao();
		memberVo = dao.selectAccount(memberVo);
		// memberVo has a user's home info that the user logging in now accessed.
		
		page = 1;
		selectPhoto();
		
		sanghyunDao dao2 = new sanghyunDao();
	    savedFile = dao2.getProfile(memberVo.getProfile_id());
		
		// loading the user who is loggin in now.
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		MemberVo temp = (MemberVo)session.get("login");
		if(temp != null){
			loginId = temp.getEmail();
			if(!loginId.equals(memberVo.getEmail())){
				checkRequest(email, loginId);
				return "other";
			}
		}
		return SUCCESS;
	}
	
	
	/*
	 * ACCOUNT ACTION
	 */
	
	// load the user's photos
	public String selectPhoto(){
		jinkyuDao dao = new jinkyuDao();
		
		PhotoVo vo = new PhotoVo();
		vo.setEmail(email);
		vo.setPage(page);
		
		photoList = dao.selectPhoto(vo);
		
		return SUCCESS;
	}
	
	// load one photo
	public String loadPhoto(){
		System.out.println("loadPhoto method activated");
		return SUCCESS;
	}
	
	
	/*
	 * FRIENDS ACTION
	 */
	
	// search member
	public String searchMember(){
		System.out.println("searchMember method activated");
		jinkyuDao dao = new jinkyuDao();
		searchList = dao.searchMember(keyword);
		
		return SUCCESS;
	}
	
	// search members in detail
	public String searchMemberDetail(){
		System.out.println("searchMemberDetail method activated");
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		MemberVo temp = (MemberVo)session.get("login");
		searchVo.setEmail(temp.getEmail());
		
		jinkyuDao dao = new jinkyuDao();
		searchDetailList = dao.searchMemberDetail(searchVo);
		
		return SUCCESS;
	}
	
	public String goFindFriend(){
		System.out.println("goFindFriend method activated");
		jinkyuDao dao = new jinkyuDao();
		
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		MemberVo temp = (MemberVo)session.get("login");
		loginId = temp.getEmail();
		
		friendUpdateList = dao.selectFriendsAlerts(loginId);
		myRequestList = dao.selectMyRequest(loginId);
		
		return SUCCESS;
	}
	
	public String friendAllow(){
		System.out.println("friendAllow method activated");
		
		jinkyuDao dao = new jinkyuDao();
		FriendUpdateVo vo = new FriendUpdateVo();
		vo.setId(friendId);
		vo.setRequestEmail(requestEmail);
		
		int result = dao.friendAllow(vo);
		
		return SUCCESS;
	}
	
	public String sendRequest(){
		System.out.println("sendRequest method activated");
		
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		MemberVo temp = (MemberVo)session.get("login");
		
		jinkyuDao dao = new jinkyuDao();
		FriendUpdateVo vo = new FriendUpdateVo();
		vo.setEmail(email);					// reciever
		vo.setRequestEmail(temp.getEmail());	// sender
		
		dao.sendRequest(vo);
		
		return SUCCESS;
	}
	
	// this method checks the relationship of other users
	public void checkRequest(String email, String requestEmail){
		System.out.println("checkRequest method activated");
		
		jinkyuDao dao = new jinkyuDao();
		FriendUpdateVo vo = new FriendUpdateVo();
		vo.setEmail(email);					// reciever
		vo.setRequestEmail(requestEmail);	// sender
		
		friendVo = dao.checkRequest(vo);
		System.out.println("friendVo: "+friendVo);
	}
	
	
	
	// == value objects below this area ==
	public MemberVo getMemberVo() {
		return memberVo;
	}

	public void setMemberVo(MemberVo memberVo) {
		this.memberVo = memberVo;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List<PhotoVo> getPhotoList() {
		return photoList;
	}

	public void setPhotoList(List<PhotoVo> photoList) {
		this.photoList = photoList;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getLoginId() {
		return loginId;
	}

	public void setLoginId(String loginId) {
		this.loginId = loginId;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public List<SearchVo> getSearchList() {
		return searchList;
	}

	public void setSearchList(List<SearchVo> searchList) {
		this.searchList = searchList;
	}

	public String getSavedFile() {
		return savedFile;
	}

	public void setSavedFile(String savedFile) {
		this.savedFile = savedFile;
	}

	public List<FriendUpdateVo> getFriendUpdateList() {
		return friendUpdateList;
	}

	public void setFriendUpdateList(List<FriendUpdateVo> friendUpdateList) {
		this.friendUpdateList = friendUpdateList;
	}

	public String getRequestEmail() {
		return requestEmail;
	}

	public void setRequestEmail(String requestEmail) {
		this.requestEmail = requestEmail;
	}

	public int getFriendId() {
		return friendId;
	}

	public void setFriendId(int friendId) {
		this.friendId = friendId;
	}

	public FriendUpdateVo getFriendVo() {
		return friendVo;
	}

	public void setFriendVo(FriendUpdateVo friendVo) {
		this.friendVo = friendVo;
	}

	public List<FriendUpdateVo> getMyRequestList() {
		return myRequestList;
	}

	public void setMyRequestList(List<FriendUpdateVo> myRequestList) {
		this.myRequestList = myRequestList;
	}

	public SearchVo getSearchVo() {
		return searchVo;
	}

	public void setSearchVo(SearchVo searchVo) {
		this.searchVo = searchVo;
	}

	public List<SearchVo> getSearchDetailList() {
		return searchDetailList;
	}

	public void setSearchDetailList(List<SearchVo> searchDetailList) {
		this.searchDetailList = searchDetailList;
	}
	
}
